package com.ssm.service;

import com.alibaba.fastjson.JSON;
import com.hqj.HightChartUtil;
import com.ssm.entity.Commons;
import com.ssm.entity.Down;
import com.ssm.entity.Files;
import com.ssm.entity.TypeNameEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HighchartServiceI {

    @Resource(name="filesServiceI")
    private FilesServiceI fsi;
    @Resource(name="downServiceI")
    private DownServiceI dsi;

    public String  getPie(){
        //54854flklfssghtrgr map统计每一个字符出现的次数

        String text = "各种文件数量饼图";

        List<Map<String,Object>> data = new ArrayList<Map<String, Object>>();

        //数据库获取数据
        Files fs = new Files();
        fs.setFileType("file");
        List<Files> list = fsi.find(fs);
        Map<String,Integer> types = new HashMap<String, Integer>();
        for(Files f:list){
            int num = 1;
            String key = f.getTypeName();
            //判断key是否已经存在在types中
            if(types.containsKey(key)){
                num = types.get(key);
                num++;
            }
            types.put(key,num);
        }
        boolean flat = true;
        for(String key:Commons.TYPE_NAMES) {
            int value = 0;
            //判断所有类型中的任意一个是否在types集合中，在则获取数据否则为0
            if(types.containsKey(key)){
                value = types.get(key);
            }
            int sum = list.size();
            //计算每一种类型的占比
            double y = Math.round((value / (sum*1.0D))*10000)/100.0D;
            Map<String, Object> m = new HashMap<String, Object>();
            m.put("name", TypeNameEnum.getValue(key));
            m.put("y", y);
            if(flat&&y>0) {
                flat = false;
                m.put("sliced", true);
                m.put("selected", true);
            }
            data.add(m);
        }


        String json = JSON.toJSONString(data);

        return HightChartUtil.createPie(text,json);
    }

    public String getBLC(){
        Calendar c1 = Calendar.getInstance();
        String text = c1.get(1)+"年"+(c1.get(2)+1)+"月每日的上传下载量";
        String ytext = "上传下载量(次)";
        String cs[] = new String[0];
        //按照当前月份第一天
        c1.set(c1.get(1),c1.get(2),1,0,0,0);
        //获取最后一天
        Calendar c2 = Calendar.getInstance();
        c2.add(2,1);
        c2.set(5,0);
        c2.set(c2.get(1),c2.get(2),c2.get(5),23,59,59);
        int a = 1;
        while(true){
            cs = Arrays.copyOf(cs,cs.length+1);
            cs[cs.length-1] = a<10?"0"+a:a+"";
            if(a==c2.get(5)) break;
            a++;
        }
        String categories = JSON.toJSONString(cs);

        List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();

        String names[] = {"上传","下载"};
        for(String name:names) {
            Map<String, Object> m = new HashMap<String, Object>();
            Map<String,Integer> map = new HashMap<String, Integer>();
            if("上传".equals(name)) {
                map = new HashMap<String, Integer>();
                Files files = new Files();
                files.setFileType("file");
                files.setStartTime(c1.getTime());
                files.setEndTime(c2.getTime());
                List<Files> fs = fsi.find(files);
                for (Files f : fs) {
                    int i = 1;
                    Date createTime = f.getCreateTime();
                    SimpleDateFormat sdf = new SimpleDateFormat("dd");
                    String key = sdf.format(createTime);
                    if (map.containsKey(key)) {
                        i = map.get(key);
                        i++;
                    }
                    map.put(key, i);
                }

            }else{
                map = new HashMap<String, Integer>();
                Down d = new Down();
                d.setStartTime(c1.getTime());
                d.setEndTime(c2.getTime());
                List<Down> ds = dsi.find(d);
                for (Down down : ds) {
                    int i = 1;
                    Date createTime = down.getDownTime();
                    SimpleDateFormat sdf = new SimpleDateFormat("dd");
                    String key = sdf.format(createTime);
                    if (map.containsKey(key)) {
                        i = map.get(key);
                        i++;
                    }
                    map.put(key, i);
                }
            }
            double data[] = new double[cs.length];
            for (int i = 0; i < data.length; i++) {
                Integer value = map.get((i + 1) < 10 ? "0" + (i + 1) : (i + 1) + "");
                data[i] = value == null ? 0 : value.intValue();
            }
            m.put("name", name);
            m.put("data", data);
            list.add(m);
        }
        String series = JSON.toJSONString(list);

        return HightChartUtil.createBLC(text,categories,ytext,series,"次");
    }

}
